<template>
  <div class="dialog-container">
    <el-dialog
      :title="dialogType==='add'?'添加设备与排口关系':dialogType === 'detail'?'设备与排口关系详情':'修改设备与排口关系'"
      :visible="dialogVisible"
      @open="initData"
      @close="closeDialog"
    >
      <el-tabs>
        <el-tab-pane label="未在有效期的合同">
          <el-table v-loading="tableLoading" :data="historyContractTable">
            <el-table-column prop="serviceType" label="合同性质">
              <template slot-scope="scope">{{ getDictLabel('e029', scope.row.serviceType) }}
              </template>
            </el-table-column>
            <el-table-column prop="startTime" label="合同开始时间">
              <template slot-scope="scope">
                <div>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</div>
              </template>
            </el-table-column>
            <el-table-column prop="endTime" label="合同结束时间">
              <template slot-scope="scope">
                <div>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</div>
              </template>
            </el-table-column>
            <el-table-column prop="inspectionCycle" label="巡检周期（天）" />
            <el-table-column prop="calibrationCycle" label="标定/校准周期（天）" />
            <el-table-column prop="maintenanceCycle" label="维护周期（天）" />
            <el-table-column prop="comparisonPeriod" label="实际水样比对周期（天）" />
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <el-tabs>
        <el-tab-pane label="当前合同">
          <el-table v-loading="tableLoading" :data="nowContract">
            <el-table-column prop="serviceType" label="合同性质">
              <template slot-scope="scope">{{ getDictLabel('e029', scope.row.serviceType) }}
              </template>
            </el-table-column>
            <el-table-column prop="startTime" label="合同开始时间">
              <template slot-scope="scope">
                <div>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</div>
              </template>
            </el-table-column>
            <el-table-column prop="endTime" label="合同结束时间">
              <template slot-scope="scope">
                <div>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</div>
              </template>
            </el-table-column>
            <el-table-column prop="inspectionCycle" label="巡检周期（天）" />
            <el-table-column prop="calibrationCycle" label="标定/校准周期（天）" />
            <el-table-column prop="maintenanceCycle" label="维护周期（天）" />
            <el-table-column prop="comparisonPeriod" label="实际水样比对周期（天）" />
          </el-table>
          <!-- <div
            v-for="(item, index) in nowContract"
            :key="index + '_dataForm'"
          >
            <el-form>
              <el-row>
                <el-col :span="4">
                  <el-form-item label="合同性质">
                    <el-select v-model="item.serviceType">
                      <el-option
                        v-for="(obj, sub) in serviceTypeList"
                        :key="sub + '_serviceType'"
                        :value="obj.code"
                        :label="obj.codeName"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="11">
                  <el-form-item label="合同时间">
                    <el-date-picker
                      v-model="item.startAndEndTime"
                      type="daterange"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="合同执行周期（天）">
                    <el-input v-model="item.days" />
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="操作">
                    <br>
                    <el-button size="mini" @click="handleAdd">添加</el-button>
                    <el-button size="mini" @click="handleDel(index)">删除</el-button>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div> -->
        </el-tab-pane>
      </el-tabs>
      <span v-if="dialogType !== 'detail'" slot="footer" class="dialog-footer">
        <el-button class="sure-btn" :loading="saveLoading" size="mini" type="primary" @click="handleSave">确定</el-button>
        <el-button class="cancel-btn" size="mini" @click="closeDialog">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getDict } from '@/utils/dict'
import { getPage } from '@/api/base/tContractOe'
import { getDictLabel } from '@/utils'
import { parseTime } from '@/utils/index'

export default {
  name: 'MaintenanceContractsDialog',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    dialogType: {
      type: String,
      default: 'detail'
    },
    outletEquipmentId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      historyContractTable: [],
      nowContract: [],
      serviceTypeList: [],
      tableLoading: false,
      saveLoading: false
    }
  },
  created() {
    this.serviceTypeList = getDict('e029')
  },
  mounted() {},
  methods: {
    getDictLabel, parseTime,
    initData() {
      this.handleSelect()
    },
    // 查询
    handleSelect() {
      this.tableLoading = true
      if (this.outletEquipmentId) {
        getPage({ outletEquipmentId: this.outletEquipmentId }).then(res => {
          const historyContractTable = []
          const nowContract = []
          res.data.rows.forEach(item => {
            if (item.lastContract === 1) {
              item.startAndEndTime = [item.startTime, item.endTime]
              nowContract.push(item)
            } else {
              item.startAndEndTime = [item.startTime, item.endTime]
              historyContractTable.push(item)
            }
            this.historyContractTable = historyContractTable
            this.nowContract = nowContract
          })
          this.tableLoading = false
        }).catch(() => {
          this.tableLoading = false
        })
      }
    },
    handleAdd() {
      this.nowContract.push(
        {
          serviceType: '',
          startAndEndTime: '',
          inspectionCycle: '',
          calibrationCycle: '',
          maintenanceCycle: '',
          comparisonPeriod: ''
        }
      )
    },
    handleDel(index) {
      if (this.nowContract.length === 1) {
        this.$message.info('已经是最后一个')
        return
      }
      this.nowContract.splice(index, 1)
    },
    handleSave() {
      this.$emit('selectContract', this.nowContract)
    },
    closeDialog() {
      this.$emit('closeContractDialog')
    }
  }
}
</script>

<style lang='scss' scoped>
</style>
